import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:geomancy/enums/index.dart';
import 'package:geomancy/models/base_model.dart';
import 'package:geomancy/models/common_model.dart';
import 'package:geomancy/models/record_list_model.dart';
import 'package:geomancy/tools/http.dart';

class GeomancyAPI {
  ///新增堪舆记录
  static Future<CommonModel?> addRecord({required BuildContext context, required GeomancyAddAPIData params}) async {
    try {
      final data = await Fetch.request(Method.POST, '/records/add', params: params.toJson(), context: context);
      return CommonModel.fromJson(json.decode(data.data));
    } catch(e) {
      return null;
    }
  }
  ///获取堪舆记录
  static Future<RecordResultModel?> getRecordList({required BuildContext context, required CommonPage page}) async {
    try {
      final data = await Fetch.request(Method.POST, '/records/list', params: page.toJson(), context: context);
      final result = RecordListModel.fromJson(json.decode(data.data));
      if (result.code == 200) {
        return result.data;
      }
      return null;
    } catch(e) {
      return null;
    }
  }
  ///根据类型获取最近一条记录详情
  static Future<RecordItemModel?> getLastRecordDetail(BuildContext context, CompassSource source) async {
    try {
      final type = compassSourceValues[source];
      final data = await Fetch.request(Method.GET, '/records/getRecordsByType/$type', context: context);
      final result = CommonModel.fromJson(json.decode(data.data));
      if (result.code == 200) {
        final RecordItemModel item = RecordItemModel.fromJson(result.data);
        return item;
      }
      return null;
    } catch(e) {
      return null;
    }
  }
}